<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="酒店名称" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" clearable placeholder="请输入酒店名称" />
                </el-form-item>
                <el-form-item label="城市" prop="city">
                    <el-cascader
                        v-model="selectedCity"
                        :options="cities"
                        :props="{
                            value: 'value',
                            label: 'text',
                            children: 'children',
                            checkStrictly: true
                        }"
                        placeholder="请选择省市区"
                        clearable
                        @change="handleCityChange"
                    />
                </el-form-item>
                <!-- <el-form-item label="省份" prop="province">
                    <el-input class="w-[280px]" v-model="queryParams.province" clearable placeholder="请输入省份" />
                </el-form-item>
                <el-form-item label="城市" prop="city">
                    <el-input class="w-[280px]" v-model="queryParams.city" clearable placeholder="请输入城市" />
                </el-form-item>
                <el-form-item label="区/县" prop="district">
                    <el-input class="w-[280px]" v-model="queryParams.district" clearable placeholder="请输入区" />
                </el-form-item> -->
                <!-- <el-form-item label="详细地址" prop="address">
                    <el-input class="w-[280px]" v-model="queryParams.address" clearable placeholder="请输入详细地址" />
                </el-form-item>
                <el-form-item label="封面图" prop="cover">
                    <el-input class="w-[280px]" v-model="queryParams.cover" clearable placeholder="请输入封面图" />
                </el-form-item>
                <el-form-item label="轮播图" prop="photos">
                    <el-input class="w-[280px]" v-model="queryParams.photos" clearable placeholder="请输入轮播图" />
                </el-form-item>
                <el-form-item label="联系电话" prop="phone">
                    <el-input class="w-[280px]" v-model="queryParams.phone" clearable placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="简介" prop="desc">
                    <el-input class="w-[280px]" v-model="queryParams.desc" clearable placeholder="请输入简介" />
                </el-form-item>
                <el-form-item label="最低价格" prop="price">
                    <el-input class="w-[280px]" v-model="queryParams.price" clearable placeholder="请输入最低价格" />
                </el-form-item>
                <el-form-item label="最低原价" prop="origin_price">
                    <el-input class="w-[280px]" v-model="queryParams.origin_price" clearable placeholder="请输入最低原价" />
                </el-form-item>
                <el-form-item label="经度" prop="longitude">
                    <el-input class="w-[280px]" v-model="queryParams.longitude" clearable placeholder="请输入经度" />
                </el-form-item>
                <el-form-item label="维度" prop="latitude">
                    <el-input class="w-[280px]" v-model="queryParams.latitude" clearable placeholder="请输入维度" />
                </el-form-item>
                <el-form-item label="1 正常 0 删除" prop="status">
                    <el-input class="w-[280px]" v-model="queryParams.status" clearable placeholder="请输入1 正常 0 删除" />
                </el-form-item>
                <el-form-item label="" prop="ctime">
                    <el-input class="w-[280px]" v-model="queryParams.ctime" clearable placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="utime">
                    <el-input class="w-[280px]" v-model="queryParams.utime" clearable placeholder="请输入" />
                </el-form-item> -->

                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['hotel/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button
                v-perms="['hotel/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="酒店名称" prop="title" show-overflow-tooltip />
                    <el-table-column label="省份" prop="province_name" show-overflow-tooltip />
                    <el-table-column label="城市" prop="city_name" show-overflow-tooltip />
                    <el-table-column label="区/县" prop="district_name" show-overflow-tooltip />
                    <el-table-column label="详细地址" prop="address" show-overflow-tooltip />
                    <el-table-column label="封面图" prop="cover" show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-image :src="row.cover" style="width: 100px; height: 100px;" :fit="row.cover.includes('http') ? 'cover' : 'contain'"/>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="轮播图" prop="photos" show-overflow-tooltip /> -->
                    <el-table-column label="联系电话" prop="phone" show-overflow-tooltip />
                    <!-- <el-table-column label="简介" prop="desc" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="最低价格" prop="price" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="最低原价" prop="origin_price" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="经度" prop="longitude" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="维度" prop="latitude" show-overflow-tooltip /> -->
                    <el-table-column label="状态" prop="status_txt" show-overflow-tooltip />
                    <!-- <el-table-column label="创建时间" prop="create_time" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="更新时间" prop="update_time" show-overflow-tooltip /> -->

                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                             <el-button
                                v-perms="['hotel/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button v-if="row.status_txt == '正常'"
                                v-perms="['hotel/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id, 'delete')"
                            >
                                删除
                            </el-button>
                            <el-button v-else
                                v-perms="['hotel/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id, 'restore')"
                            >
                                恢复
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="hotelLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiHotelLists, apiHotelDelete, apiHotelRestore } from '@/api/hotel'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { cities } from '@/utils/citys'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    title: '',
    city: '',
    province: '',
    district: '',
    address: '',
    cover: '',
    photos: '',
    phone: '',
    desc: '',
    price: '',
    origin_price: '',
    longitude: '',
    latitude: '',
    status: '',
    ctime: '',
    utime: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiHotelLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    // editRef.value?.setFormData(data)
    editRef.value?.getDetail({ id: data.id })
}

// 删除
const handleDelete = async (id: number | any[], type: string) => {
    if (type == 'delete') {
        await feedback.confirm('确定要删除？')
        await apiHotelDelete({ id })
        getLists()
    } else {
        await feedback.confirm('确定要恢复？')
        await apiHotelRestore({ id })
        getLists()
    }
}

// 城市选择相关
const selectedCity = ref<string[]>([])
// 处理城市选择变化
const handleCityChange = (value: string[]) => {
    if (value && value.length === 3) {
        const [provinceCode, cityCode, districtCode] = value
        const province = cities.find(p => p.value === provinceCode)
        const city = province?.children?.find(c => c.value === cityCode)
        const district = city?.children?.find(d => d.value === districtCode)

        queryParams.province = province?.value || ''
        queryParams.city = city?.value || ''
        queryParams.district = district?.value || ''
    } else {
        queryParams.province = ''
        queryParams.city = ''
        queryParams.district = ''
    }
}
getLists()
</script>

